//
//  ReadDateListViewController.m
//  product_B
//
//  Created by lanou on 16/5/30.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "ReadDateListViewController.h"
#import "ReadMainModel.h"
#import "ReadserialModel.h"
#import "ReadQuestionModel.h"
#import "ReadMainTableViewCell.h"
#import "ReadTableViewController.h"
@interface ReadDateListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *modelArray;
@end

@implementation ReadDateListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = [self.url substringFromIndex:self.url.length - 7];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight-64 + 49 )];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [self requestWork];
    [self.tableView registerNib:[UINib nibWithNibName:@"ReadMainTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 2;
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)requestWork{
    [RequestManager initWithUrlString:self.url requestType:@"GET" parDic:nil finishBlock:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        if (_index == 0) {
            self.modelArray = [ReadMainModel readDetailModelWithConfigure:dic];
        }else if (_index == 1){
            self.modelArray = [ReadserialModel readDetailSerialModelWithConfigure:dic];
        }else{
            self.modelArray = [ReadQuestionModel readDetailQuestionModelWithConfigure:dic];
        }
        [self.tableView reloadData];
    } errorBlock:^(NSError *error) {
        
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *array = @[@"短篇",@"连载",@"问答"];
    if (_index == 0) {
        ReadMainModel *model = _modelArray[indexPath.row];
        
        cell.titleLabel.text = model.hp_title;
        NSDictionary *dic = model.author[0];
        cell.authorLabel.text = dic[@"user_name"];
        cell.contentLabel.text= model.guide_word;
    }else if (_index == 1){
        ReadserialModel *model = _modelArray[indexPath.row];
        cell.titleLabel.text  = model.title;
        cell.authorLabel.text = model.author[@"user_name"];
        cell.contentLabel.text = model.excerpt;
    }else{
        ReadQuestionModel *model = _modelArray[indexPath.row];
        cell.titleLabel.text = model.question_title;
        cell.authorLabel.text = model.answer_title;
        cell.contentLabel.text = model.answer_content;
    }
    [cell.button setTitle:array[_index] forState:UIControlStateNormal];
    cell.button.layer.borderColor = [UIColor blueColor].CGColor;
    cell.button.layer.cornerRadius = 5;
    cell.button.layer.borderWidth = 1;
    cell.button.alpha = 0.5;
    cell.layer.cornerRadius = 5;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ReadTableViewController *tableVC = [[ReadTableViewController alloc]init];
    
    if (self.index == 0) {
        ReadMainModel *model = self.modelArray[indexPath.row];
        tableVC.modelID = model.content_id;
        tableVC.isRihgt = YES;
    }else if (_index == 1){
        ReadserialModel *model = self.modelArray[indexPath.row];
        tableVC.modelID = model.myID;
    }else{
        ReadQuestionModel *model = self.modelArray[indexPath.row];
        tableVC.modelID = model.question_id;
    }
    tableVC.indexY = _index;
    tableVC.isAdd = YES;
    [self.navigationController pushViewController:tableVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
